package org.shagin.jfin.date.cond;

import java.util.Calendar;
import java.util.Date;

/**
 * The Class Monthly.
 */
@SuppressWarnings("serial")
public class Monthly extends DateCondition {

    private int[] days = new int[0];

    /**
     * Instantiates a new monthly.
     * 
     * @param days the days
     */
    public Monthly(int[] days) {
        this.days = days;
    }

    /**
     * Instantiates a new monthly.
     * 
     * @param day the day
     */
    public Monthly(int day) {
        this(new int[]{day});
    }

    /**
     * Gets the days.
     * 
     * @return the days
     */
    public int[] getDays() {
        return days;
    }

    /**
     * Sets the days.
     * 
     * @param days the new days
     */
    public void setDays(int[] days) {
        this.days = days;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean validDate(Date date) {
        if (!super.validDate(date))
            return false;

        boolean result = false;
        if (days.length > 0) {
            cal.setTime(date);
            int day = cal.get(Calendar.DAY_OF_MONTH);
            for (int i = 0; i < days.length; i++) {
                if (days[i] == day) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "Ежемесячно " + toStringOrdered(days) + " " + toString(range);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String format() {
        return "Monthly(" + join(days) + ")";
    }
}
